using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace TesteColisao
{
  
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Texture2D ken;
        Texture2D evilKen;
        Texture2D cenario;

        const int largura = 66;
        const int altura = 98;
        const int framesPorSegundo = 12;
        const int qtdDesenhoAnimado = 6;
        const float velocidade = 0.2f;

        Vector2 evilKenPosXY;
        Vector2 kenPosXY;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.PreferredBackBufferWidth = 768;
            graphics.PreferredBackBufferHeight = 240;
        }

 
        protected override void Initialize()
        {

            evilKenPosXY = new Vector2(300, 130);
            kenPosXY = new Vector2(100, 130);
            base.Initialize();
        }

        protected override void LoadContent()
        {
            
            spriteBatch = new SpriteBatch(GraphicsDevice);

            ken = Content.Load<Texture2D>("Ken");
            evilKen = Content.Load<Texture2D>("EvilKen");
            cenario = Content.Load<Texture2D>("Stage_Ken");
        }

       
        protected override void UnloadContent()
        {
           
        }

       
        protected override void Update(GameTime gameTime)
        {
            KeyboardState key = Keyboard.GetState();

            float deslocamento = (float)(gameTime.ElapsedGameTime.TotalMilliseconds) * velocidade;

            if (key.IsKeyDown(Keys.Right))
            {
                if (!testaColisao())
                {
                    kenPosXY.X += deslocamento;
                }
            }

            if (key.IsKeyDown(Keys.Left))
            {

                kenPosXY.X -= deslocamento;
            }

            

            base.Update(gameTime);
        }

       
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();

            int frame = (int)(gameTime.TotalGameTime.TotalSeconds * framesPorSegundo) % qtdDesenhoAnimado;
            
            spriteBatch.Draw(cenario, Vector2.Zero, Color.White);
            spriteBatch.Draw(ken, kenPosXY, new Rectangle(frame * largura, 0, largura, altura), Color.White);
            spriteBatch.Draw(ken, evilKenPosXY, new Rectangle(frame * largura, 0, largura, altura), Color.OliveDrab,0f,
                Vector2.Zero,1f, SpriteEffects.FlipHorizontally,0.1f);

            spriteBatch.End();

            base.Draw(gameTime);
        }

        private  bool testaColisao()
        {
            int diferencaColisao = 30;
            Rectangle kenColisao = new Rectangle((int)kenPosXY.X, (int)kenPosXY.Y, largura, altura);
            Rectangle evilKenColisao = new Rectangle((int)evilKenPosXY.X + diferencaColisao, (int)evilKenPosXY.Y + diferencaColisao
                , largura - diferencaColisao, altura - diferencaColisao);

            if (kenColisao.Intersects(evilKenColisao))
            {
                return true;

            }

            else
            {
                return false;
            }

        }
       
    }

    
}
